// pages/person/personApply.js
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify'
import WxValidate from '../../utils/WxValidate.js'
import { biztitleareaList,uploadIdCard,sendSms,sub,querySelfBusinessInfo } from '../../api/personApply.js'
let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showSendSmsBtn: false,
    showEducationPicker: false,
    showBusinessLicensePicker: false,
    disabledSubBtn: false,
    subBtnHtml: '提交',
    imagewidth: 0,
    imageYQwidth: 0,
    imageheight: 0,
    imageYQheight: 0,
    imageMarginLeft: 0,
    imageYQMarginLeft: 0,
    businessLicenseArr: [],
    idCardZM: '../../images/idCardZM.png',
    idCardFM: '../../images/idCardFM.png',
    payStatus: 1,
    status: 1,
    form: {
      username: '',
      mobile: '',
      education: '',
      eMail: '',
      smsCode: '',
      politics: '群众',
      businessLicenseName: '',
      businessLicenseId: '',
      operatorArea: '',
      idCardPositive: '',
      idCardOtherSide: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate()//验证规则函数
  },



  // 图片上传
  afterRead(event) {
  
    const type = event.currentTarget.dataset.type;
    const { file } = event.detail;
    uploadIdCard(file.path, 4).then(res => {
      console.log(res.data)
      var cityjson = res;
      const result = cityjson

      if(result.code == '0' || result.code == 0){
        if(type == 1){
          //正面找
          const form = this.data.form
          form.idCardPositive = result.data.id
          this.setData({
            idCardZM: result.data.url,
            form: form
          })
        }else {
          //反面照
          const form = this.data.form
          form.idCardOtherSide = result.data.id 
          this.setData({
            idCardFM: result.data.url,
            form: form
          })
        }
      } else {
        console.log(111)
      }
    }).catch(err=>{
     console.log(err)
    });
  },
  /**
   * 初始化规则验证
   */
  initValidate() {
    const rules = {
      username: {
        required: true,
        minlength:2
      },
      bankcard:{
        required: true,
        digits:true
      }

    }
    const messages = {
      username: {
        required: '您的姓名必填',
        minlength:'请输入正确的姓名'
      },
      bankcard: {
        required: '银行卡号必填',
        digits:'请输入正确的银行卡号'
      },
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  formSubmit: function(e) {
    // 提交禁用按钮
    this.setData({
      disabledSubBtn: true,
      subBtnHtml: '提交材料中.....'
    })

    const params = e.detail.value
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      Notify({ type: 'danger', message: error.msg })
      this.disabledSubBtnFalse()
      return false
    }

    const dataParams = {
      'userName': this.data.form.username,
      'mobile':  app.globalData.mobile,
      'idCardPositive':this.data.form.idCardPositive,
      'idCardOtherSide':this.data.form.idCardOtherSide,
      'bankCard': this.data.form.bankcard
    }

    sub(dataParams).then(res => {
      this.disabledSubBtnFalse()
      console.log(res)
      if(res.code == 0 || res.code == '0'){
          wx.switchTab({
            url: '/pages/index/index'
          })
      }
    }).catch(err=>{
      this.disabledSubBtnFalse()
      Notify({ type: 'danger', message: err })
    });
  },
  disabledSubBtnFalse: function() {
    this.setData({
      disabledSubBtn: false,
      subBtnHtml: '提交'
    })
  },
  /**
   * 发送短信验证码
   */
 
  /***********************文本框输入监听****************************/
  userChange: function(e) {
    const form = this.data.form;
    form.username = e.detail;
    this.setData({
      form: form
    })
  },
  bankcardChange: function(e) {
    const form = this.data.form;
    form.bankcard = e.detail;
    this.setData({
      form: form
    })
  },
  /***************************图片等比缩放********************************* */
  imageLoad: function (e) {
    var originalWidth; 
    wx.getSystemInfo({ 
        success: function (res) { 
         originalWidth = res.windowWidth; 
        } 
       }) 

    this.setData({
      imagewidth: (originalWidth * 0.47),
      imageheight: (originalWidth * 0.48 * 0.7),
      imageMarginLeft: (originalWidth * 0.02)
    })
  },
  imageYQLoad: function (e) {
    var originalWidth; 
    wx.getSystemInfo({ 
        success: function (res) { 
         originalWidth = res.windowWidth; 
        } 
       }) 
    this.setData({
      imageYQwidth: (originalWidth * 0.97),
      imageYQheight: (originalWidth * 0.97 * 0.3),
      imageYQMarginLeft: (originalWidth * 0.01)
    })
  },
  /**
   * 文化程度选择
   */

  onConfirm(event) {
    const type = event.currentTarget.dataset.type
    const { picker, value, index } = event.detail;
    if(type == 1){
      const dataForm = this.data.form
      dataForm.education = value 
      this.setData({
        showEducationPicker: false,
        form: dataForm
      })
    }
    if(type == 2){
      const dataForm = this.data.form
      dataForm.businessLicenseName = value.title
      dataForm.businessLicenseId = value.id
      dataForm.operatorArea = value.bizArea
      
      this.setData({
        showBusinessLicensePicker: false,
        form: dataForm
      })
    }
  },
  onCancel(event) {
    const type = event.currentTarget.dataset.type
    if(type == 1){
      this.setData({
        showEducationPicker: false
      })
    }
    if(type == 2){
      this.setData({
        showBusinessLicensePicker: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that=this;
    wx.getStorage({
      key: 'mobile',
      success:function(res){
        app.globalData.mobile = res.data
       
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
function countdown(that) {
  var second = that.data.second;
  if (second == 0) {
    that.setData({
      second: 60,
      showSendSmsBtn: false,
      showSendSmsBtnHtml: "发送验证码"
    });
    return;
  }
  var time = setTimeout(function () {
    that.setData({
      second: second - 1,
      showSendSmsBtnHtml: second + "s后重新获取"
    });
    countdown(that);
  }, 1000)
}